package org.inspiration.dudu.user.service.impl;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.user.mapper.FavoriteFolderMapper;
import org.inspiration.dudu.user.model.dto.FavoriteFolderDTO;
import org.inspiration.dudu.user.model.entity.FavoriteFolderEntity;
import org.inspiration.dudu.user.model.vo.FavoriteFolderVO;
import org.inspiration.dudu.user.service.FavoriteFolderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏夹服务实现类
 * 提供对用户收藏夹的增删查改操作
 */
@Service
@RequiredArgsConstructor
public class FavoriteFolderServiceImpl implements FavoriteFolderService {

    private final FavoriteFolderMapper favoriteFolderMapper;

    /**
     * 创建新的收藏夹
     * 
     * @param favoriteFolderDTO 收藏夹数据传输对象
     */
    @Override
    public void createFavoriteFolder(FavoriteFolderDTO favoriteFolderDTO) {
        favoriteFolderMapper.createFavoriteFolder(transToEntity(favoriteFolderDTO));
    }

    /**
     * 将收藏夹数据传输对象转换为实体对象
     * 
     * @param favoriteFolderDTO 收藏夹数据传输对象
     * @return 收藏夹实体对象
     */
    @Override
    public FavoriteFolderEntity transToEntity(FavoriteFolderDTO favoriteFolderDTO) {
        return FavoriteFolderEntity.builder()
                .userId(favoriteFolderDTO.getUserId())
                .folderName(favoriteFolderDTO.getFolderName())
                .createdAt(LocalDateTime.now()).build();
    }

    /**
     * 删除指定的收藏夹
     * 
     * @param favoriteFolderDTO 收藏夹数据传输对象
     */
    @Override
    public void deleteFavoriteFolder(FavoriteFolderDTO favoriteFolderDTO) {
        favoriteFolderMapper.deleteFavoriteFolder(favoriteFolderDTO);
    }

    /**
     * 根据用户ID获取所有收藏夹
     * 
     * @param userId 用户ID
     * @return 收藏夹视图对象列表
     */
    @Override
    public List<FavoriteFolderVO> getAllFavoriteFoldersByUserId(Long userId) {
        List<FavoriteFolderEntity> list = favoriteFolderMapper.getAllFavoriteFoldersByUserId(userId);
        List<FavoriteFolderVO> res = new ArrayList<>();
        for(FavoriteFolderEntity entity: list) {
            res.add(transToVO(entity));
        }
        return res;
    }

    /**
     * 将收藏夹实体对象转换为视图对象
     * 
     * @param favoriteFolderEntity 收藏夹实体对象
     * @return 收藏夹视图对象
     */
    @Override
    public FavoriteFolderVO transToVO(FavoriteFolderEntity favoriteFolderEntity) {
        FavoriteFolderVO favoriteFolderVO = new FavoriteFolderVO();
        BeanUtils.copyProperties(favoriteFolderEntity, favoriteFolderVO);
        return favoriteFolderVO;
    }

    /**
     * 增加收藏夹的内容数量
     * 
     * @param folderId 收藏夹ID
     */
    @Override
    public void increaseContentCount(Long folderId) {
        favoriteFolderMapper.increaseContentCount(folderId);
    }

    /**
     * 减少收藏夹的内容数量
     * 
     * @param folderId 收藏夹ID
     */
    @Override
    public void decreaseContentCount(Long folderId) {
        favoriteFolderMapper.decreaseContentCount(folderId);
    }
}
